package com.cook.controller;

import com.cook.dto.recipes.IngredientDTO;
import com.cook.result.Result;
import com.cook.service.IngredientsService;
import com.cook.vo.recipes.IngredientVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ingredients")
@Slf4j
public class IngredientsController {
    @Autowired
    private IngredientsService ingredientsService;

    /**
     * 根据食谱ID查询食材列表
     */
    @GetMapping("/recipe/{recipeId}")
    public Result<List<IngredientVO>> getIngredientsByRecipeId(@PathVariable Long recipeId) {
        log.info("查询食谱食材: recipeId={}", recipeId);

        try {
            List<IngredientVO> ingredients = ingredientsService.getIngredientsByRecipeId(recipeId);
            return Result.success(ingredients);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询食材失败: ", e);
            return Result.error(500, "查询食材失败");
        }
    }

    /**
     * 根据食材ID查询食材详情
     */
    @GetMapping("/{ingredientId}")
    public Result<IngredientVO> getIngredientById(@PathVariable Long ingredientId) {
        log.info("查询食材详情: ingredientId={}", ingredientId);

        try {
            IngredientVO ingredient = ingredientsService.getIngredientById(ingredientId);
            return Result.success(ingredient);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询食材详情失败: ", e);
            return Result.error(500, "查询食材失败");
        }
    }

    /**
     * 添加食材
     */
    @PostMapping
    public Result<Long> addIngredient(@RequestBody IngredientDTO ingredientDTO) {
        log.info("添加食材: recipeId={}, name={}", ingredientDTO.getRecipeId(), ingredientDTO.getName());

        try {
            Long ingredientId = ingredientsService.addIngredient(ingredientDTO);
            return Result.success("食材添加成功", ingredientId);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("添加食材失败: ", e);
            return Result.error(500, "添加食材失败");
        }
    }

    /**
     * 批量添加食材
     */
    @PostMapping("/batch")
    public Result<Boolean> batchAddIngredients(@RequestBody List<IngredientDTO> ingredientDTOList) {
        log.info("批量添加食材: 共 {} 种", ingredientDTOList.size());

        try {
            boolean success = ingredientsService.batchAddIngredients(ingredientDTOList);
            if (success) {
                return Result.success("食材批量添加成功", true);
            } else {
                return Result.error("食材批量添加失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("批量添加食材失败: ", e);
            return Result.error(500, "批量添加食材失败");
        }
    }

    /**
     * 更新食材
     */
    @PutMapping
    public Result<Boolean> updateIngredient(@RequestBody IngredientDTO ingredientDTO) {
        log.info("更新食材: ingredientId={}", ingredientDTO.getIngredientId());

        try {
            boolean success = ingredientsService.updateIngredient(ingredientDTO);
            if (success) {
                return Result.success("食材更新成功", true);
            } else {
                return Result.error("食材更新失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("更新食材失败: ", e);
            return Result.error(500, "更新食材失败");
        }
    }

    /**
     * 删除食材
     */
    @DeleteMapping("/{ingredientId}")
    public Result<Boolean> deleteIngredient(@PathVariable Long ingredientId) {
        log.info("删除食材: ingredientId={}", ingredientId);

        try {
            boolean success = ingredientsService.deleteIngredient(ingredientId);
            if (success) {
                return Result.success("食材删除成功", true);
            } else {
                return Result.error("食材删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除食材失败: ", e);
            return Result.error(500, "删除食材失败");
        }
    }

    /**
     * 删除食谱的所有食材
     */
    @DeleteMapping("/recipe/{recipeId}")
    public Result<Boolean> deleteIngredientsByRecipeId(@PathVariable Long recipeId) {
        log.info("删除食谱所有食材: recipeId={}", recipeId);

        try {
            boolean success = ingredientsService.deleteIngredientsByRecipeId(recipeId);
            if (success) {
                return Result.success("食谱食材删除成功", true);
            } else {
                return Result.error("食谱食材删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除食谱食材失败: ", e);
            return Result.error(500, "删除食材失败");
        }
    }

    /**
     * 复制食材到新食谱
     */
    @PostMapping("/copy/{sourceRecipeId}/to/{targetRecipeId}")
    public Result<Boolean> copyIngredientsToRecipe(
            @PathVariable Long sourceRecipeId,
            @PathVariable Long targetRecipeId) {

        log.info("复制食材: sourceRecipeId={}, targetRecipeId={}", sourceRecipeId, targetRecipeId);

        try {
            boolean success = ingredientsService.copyIngredientsToRecipe(sourceRecipeId, targetRecipeId);
            if (success) {
                return Result.success("食材复制成功", true);
            } else {
                return Result.error("食材复制失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("复制食材失败: ", e);
            return Result.error(500, "复制食材失败");
        }
    }
}
